* Varable creations : period before training -----------------------------------

	*(1) Create Pre-period outcomes
	
	preserve
		
		* (1.1) Restrict to the pre-period: defined as 2018-Jan 2020
		keep if (post == 0) & inrange(year_month, 201801, 202001)

			// The "post" condition is redundant, but we keep it in case
			// there is a change in how we identify the month periods for pre
			
			* Combined outcomes for level 1/2/3 TRRS (re-coded to match)
			
				gen l12_rc_trr_C = lv1_rc_trr_C + lv2_rc_trr_C
				gen l23_rc_trr_C = lv3_rc_trr_C + lv2_rc_trr_C
				
					* Here and throughout the code, "C" stands for "Count"
			
			* (1.3) Collapse to officer level
			gl prs
				foreach v in $prs_items {
					gl prs $prs prs_`v'
				}
				
			collapse (sum) ///
						trr_C trr_s_i_C trr_sa_injury_C trr_officer_i_C days_iod ///
						arrest_con_cop l12_rc_trr_C l23_rc_trr_C ///
						trr_s_h_C total_w_exo_accusations award ///
						trr_s_hos_i_ix_C ac_w_exo* ///
						$prs ///
						arrest_non_con_cop ///
						ttt_avoid_C ttt_phys_C ttt_force_handcuff_C , ///
			by(employee_id treatment)
			
			* (1.2) Calculating index, which is average of component scaled outcomes

			* Calculate standardized score for a number of items
				
				* PRS
				foreach v in $prs_items {
					z_cal prs_`v'
				}
				
				* Non-discretionarest and tactic used
				foreach v in ///
					arrest_non_con_cop ///
					ttt_avoid_C ttt_phys_C ttt_force_handcuff_C {
					z_cal `v'
				}
			
			* Forming composite index
			
				* PRS index, including non-discretionary arrests
				egen prs_score_w_arrs = rowmean(z_prs* z_arrest_non_con_cop)
				
				* Tactics index
					
					* Reverse sign of "avoid tactics"
					replace z_ttt_avoid_C = - z_ttt_avoid_C
				
				egen ttt_score = rowmean(z_ttt_*)
				
				
				
			rename * pre_*
			rename pre_employee_id employee_id
				
			* (1.4) Save to memory 
			tempfile pre
			save `pre'	

			* (1.5) Save to interim data for future use
			save ${Interim1}, replace
		
	restore
		
		
		
	* (2) Merge into the data
	merge m:1 employee_id using `pre', nogen keep(1 3)
	
		
		
	* (3) Indicate whether an officer has Endline assessment outcome 
		
		* (3.1) Check if there is answer in Pre/Pos/Rubrics
		foreach v in pre pos rub {
			merge m:1 survey_id using ${out_`v'}_online, ///
			keep(1 3) keepusing(survey_id startdate)
			gen answered_`v' = (_merge == 3)
			rename startdate `v'_startdate
			drop _merge
		}
		
		merge m:1 survey_id using ${out_pre}_online, ///
		keep(1 3) keepusing(online) nogen
		
			* Indicate if an officer did not attend any of the survey
			gen answered = answered_pre + answered_pos + answered_rub
			recode answered (1/max = 0) (0=1), gen(no_endline)
			
			* Calculate two index for officers with endline
			preserve
			keep if (post == 0) & inrange(year_month, 201801, 202001) & (no_endline==0)
				
				foreach v in $prs_items {
					z_cal prs_`v'
				}
				
				foreach v in ///
					arrest_non_con_cop ///
					ttt_avoid_C ttt_phys_C ttt_force_handcuff_C {
					z_cal `v'
				}
			
				egen prs_score_w_arrs_e = rowmean(z_prs* z_arrest_non_con_cop)		
				replace z_ttt_avoid_C = - z_ttt_avoid_C
				egen ttt_score_e = rowmean(z_ttt_*)


				collapse (mean) prs_score_w_arrs_e ttt_score_e , by(employee_id)

				rename * pre_*
				rename pre_employee_id employee_id
				
			tempfile pre_e
			save `pre_e'	
			restore
			
			merge m:1 employee_id using `pre_e', nogen keep(1 3)